# -*- coding: UTF-8 -*-
# 1.10 从序列中移除重复元素且保持元素间顺序不变
def test110():
    a = [1,5,2,1,9,1,5,10]
    print("hashDedpute: ")
    for x in hashDedupe(a):
        print(x)
    print("hashDepute2:")
    a = [{"x":1,"y":2},{"x":2,"y":3},{"x":1,"y":2},{"x":2,"y":4}]
    print("d['x']['y']")
    for x in hashDedupe2(a,lambda d:(d['x'],d['y'])):
        print(x)
    print("d['x']")
    for x in hashDedupe2(a,lambda d:(d['x'])):
        print(x)

# 通过哈希值过滤重复元素
def hashDedupe(items):
    seen = set()
    for item in items:
        if item not in seen:
            yield item
            seen.add(item)

# 通过哈希值或设置判断函数过滤重复元素
def hashDedupe2(items,key=None):
    seen = set()
    for item in items:
        val = item if key is None else key(item)
        if val not in seen:
            yield item
            seen.add(val)

# 切片 slice
def test111():
    items = [0,1,2,3,4,5,6]
    print(items)
    a = slice(2,4)
    print(items[a])
    items[a] = [10,11]
    print(items[a])
    del items[a]
    print(items)
    print("slice : ",a)


# 1.12 找出序列中出现次数最多的元素
# collections 中的Counter.most_common()
from collections import Counter
def test120():
    words = [
        'look','into','my','eyes','look','into','my','eyes',
        'the',"eyes",'the','eyes','the','eyes','not','around','the',
        'eyes',"don't",'look',"around",'the','eyes','look',"into",
        'my','eyes',"you're",'under'
    ]
    word_counts = Counter(words)
    top_three = word_counts.most_common(3)
    print(top_three)

if __name__ == '__main__':
    test110()
    test111()
    test120()